package org.yray.sip.base;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

public class SipServerTable {


    final Map<String,InetSocketAddress> serverMap=new HashMap();
    final Map<String,InetSocketAddress> userMap=new HashMap<>();

    private String localIp;

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    public int getLocalPort() {
        return localPort;
    }

    public void setLocalPort(int localPort) {
        this.localPort = localPort;
    }

    private int localPort;

    public void addServer(String ip,int port)
    {
        serverMap.put(ip+"_"+port,new InetSocketAddress(ip,port));
    }


    public InetSocketAddress getServerISA()
    {
        InetSocketAddress serverISA=null;
        for(Map.Entry<String,InetSocketAddress> entry:serverMap.entrySet())
        {
            serverISA= entry.getValue();
            break;
        }
        return serverISA;
    }

    public String getServerIpPort()
    {
        String ipPort=null;
        for(Map.Entry<String,InetSocketAddress> entry:serverMap.entrySet())
        {
            ipPort= entry.getKey();
            ipPort=ipPort.replace("_",":");
            break;
        }
        return ipPort;
    }

    public String getServerIp()
    {
        String ip=null;
        for(Map.Entry<String,InetSocketAddress> entry:serverMap.entrySet())
        {
            ip= entry.getKey();
            ip=ip.split("_")[0];
            break;
        }
        return ip;
    }

    public  InetSocketAddress getUserISA(String ip,int port)
    {
        String key=ip+"_"+port;
        InetSocketAddress isa=userMap.get(key);
        if(isa==null)
        {
            isa=new InetSocketAddress(ip,port);
            userMap.put(key,isa);
        }
        return isa;
    }

    public static void main(String[] args) {
        String ipPort="INVITE sip:78004@172.168.200.78_5060 SIP/2.0";
        ipPort=ipPort.replace("_",":");
        System.out.println(ipPort);
    }



}
